gRPC একটি উচ্চ-প্রদর্শন মাইক্রোসার্ভিস RPC (Remote Procedure Call) ফ্রেমওয়ার্ক, যা Protobuf ব্যবহার করে ডেটা সিরিয়ালাইজেশন করে। Spring Boot-এ gRPC Client তৈরি করার ধাপ নিচে দেয়া হলো।
Spring Boot প্রজেক্টে gRPC ব্যবহার করতে grpc-spring-boot-starter
ডিপেন্ডেন্সি যোগ করতে হবে।
Maven:
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-spring-boot-starter</artifactId>
<version>2.14.0</version>
</dependency>
Gradle:
implementation 'net.devh:grpc-spring-boot-starter:2.14.0'
gRPC সেবা ব্যবহারের জন্য .proto
ফাইল তৈরি করুন। নিচে একটি উদাহরণ দেয়া হলো:
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.example.grpc";
option java_outer_classname = "GreetingProto";
service GreetingService {
rpc Greet (GreetingRequest) returns (GreetingResponse);
}
message GreetingRequest {
string name = 1;
}
message GreetingResponse {
string message = 1;
}
Proto ফাইল থেকে Java ক্লাস জেনারেট করতে protobuf-maven-plugin
ব্যবহার করুন।
Maven Plugin Configuration:
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.21.7:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.54.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
mvn clean install
রান করলে Java ফাইলগুলো জেনারেট হবে।
Spring Boot-এ gRPC ক্লায়েন্ট তৈরি করতে আমরা @GrpcClient
এনোটেশন ব্যবহার করব।
import com.example.grpc.GreetingServiceGrpc;
import com.example.grpc.GreetingProto.GreetingRequest;
import com.example.grpc.GreetingProto.GreetingResponse;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;
@Service
public class GreetingClient {
@GrpcClient("greeting-service")
private GreetingServiceGrpc.GreetingServiceBlockingStub greetingServiceStub;
public String getGreeting(String name) {
GreetingRequest request = GreetingRequest.newBuilder()
.setName(name)
.build();
GreetingResponse response = greetingServiceStub.greet(request);
return response.getMessage();
}
}
@RestController
বা অন্য কোনও কম্পোনেন্ট থেকে gRPC ক্লায়েন্ট ব্যবহার করতে পারেন।
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
private final GreetingClient greetingClient;
public GreetingController(GreetingClient greetingClient) {
this.greetingClient = greetingClient;
}
@GetMapping("/greet")
public String greet(@RequestParam String name) {
return greetingClient.getGreeting(name);
}
}
application.yml
বা application.properties
ফাইলে gRPC সার্ভারের ঠিকানা যোগ করুন:
grpc:
client:
greeting-service:
address: "localhost:6565"
Spring Boot অ্যাপ্লিকেশন চালান এবং /greet?name=John
URL-এ অ্যাক্সেস করুন। এটি gRPC সার্ভারে একটি রিকুয়েস্ট পাঠাবে এবং সার্ভারের রেসপন্স রিটার্ন করবে।
grpc-spring-boot-starter
সহজ সমাধান দেয়।এটি ব্যবহার করে আপনার মাইক্রোসার্ভিস স্থাপনার কার্যকারিতা বাড়ানো সম্ভব।
Read more